


mod contact;

use std::ops::Add;

use cursive::Cursive;
use cursive::views:: {
    Button, Dialog, DummyView, EditView,
    TextView,
    LinearLayout,
};
use cursive::traits::*;

fn main()
{
    let mut siv = cursive::default();
    siv.add_global_callback('q', |s| s.quit());

    init_login_layer(&mut siv);

    siv.run();
}

//UI
fn init_login_layer(siv:&mut Cursive)
{
    siv.pop_layer();

    let mut view_username = LinearLayout::horizontal();
    let mut view_password = LinearLayout::horizontal();
    let mut view_buttons = LinearLayout::horizontal();

    let view_edit_username = EditView::new()
        .with_name("view_edit_username")
        .fixed_width(20)
        .max_height(1);
    let view_edit_password = EditView::new()
        .with_name("view_edit_password")
        .fixed_width(20)
        .max_height(1);

    view_username.add_child(TextView::new("Username: "));
    view_username.add_child(view_edit_username);
    
    view_password.add_child(TextView::new("Password: "));
    view_password.add_child(view_edit_password);
    
    view_buttons.add_child(Button::new("LOGIN", login));
    view_buttons.add_child(DummyView::new());
    view_buttons.add_child(Button::new("REGISTER", init_register_layer));
    view_buttons.add_child(DummyView::new());
    view_buttons.add_child(Button::new("QUIT", |s| s.quit()));
    
    let dialog_login = Dialog::around(LinearLayout::vertical()
        .child(DummyView::new())
        .child(view_username)
        .child(DummyView::new())
        .child(view_password)
        .child(DummyView::new())
        .child(view_buttons)
    ).title("User Login");

    siv.add_layer(dialog_login);
}


fn init_register_layer(siv:&mut Cursive)
{
    siv.pop_layer();

    let mut view_username = LinearLayout::horizontal();
    let mut view_password = LinearLayout::horizontal();
    let mut view_buttons = LinearLayout::horizontal();

    let view_edit_username = EditView::new()
        .with_name("view_edit_username")
        .fixed_width(20)
        .max_height(1);
    let view_edit_password = EditView::new()
        .with_name("view_edit_password")
        .fixed_width(20)
        .max_height(1);

    view_username.add_child(TextView::new("Username: "));
    view_username.add_child(view_edit_username);
    
    view_password.add_child(TextView::new("Password: "));
    view_password.add_child(view_edit_password);
    
    view_buttons.add_child(Button::new("SUBMIT", register));
    view_buttons.add_child(DummyView::new());
    view_buttons.add_child(Button::new("BACK", init_login_layer));
    
    let dialog_register = Dialog::around(LinearLayout::vertical()
        .child(DummyView::new())
        .child(view_username)
        .child(DummyView::new())
        .child(view_password)
        .child(DummyView::new())
        .child(view_buttons)
    ).title("User Register");

    siv.add_layer(dialog_register);
}

fn pop_layer(siv:&mut Cursive)
{
    siv.pop_layer();
}

fn init_dialog(siv:&mut Cursive, title:&str, text:&str)
{
    let dialog = Dialog::text(text)
        .title(title)
        .button("OK", pop_layer);
    siv.add_layer(dialog);
}


//Function
fn login(siv:&mut Cursive)
{
    let view_edit_username = siv.find_name::<EditView>("view_edit_username").unwrap();
    let view_edit_password = siv.find_name::<EditView>("view_edit_password").unwrap();
    
    let rc_username = view_edit_username.get_content();
    let rc_password = view_edit_password.get_content();

    let result = contact::login(rc_username.as_str(), rc_password.as_str());
    init_dialog(siv, "Tip:", &result);
}

fn register(siv:&mut Cursive)
{
    let view_edit_username = siv.find_name::<EditView>("view_edit_username").unwrap();
    let view_edit_password = siv.find_name::<EditView>("view_edit_password").unwrap();
    
    let rc_username = view_edit_username.get_content();
    let rc_password = view_edit_password.get_content();

    let result = contact::register(rc_username.as_str(), rc_password.as_str());
    init_dialog(siv, "Tip:", &result);
}


